<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="标签" prop="label">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="是否可用" prop="isEnable">
        <el-select v-model="queryParams.isEnable" placeholder="是否可用" clearable size="small">
          <el-option
            v-for="dict in enableOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>

      </el-form-item>

      <el-form-item label="会员身份" prop="memPerm">
        <el-select v-model="queryParams.memPerm" placeholder="会员身份" clearable size="small">
          <el-option
            v-for="dict in memPermOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>

      </el-form-item>

      <el-form-item label="注册时间">
        <el-date-picker
          v-model="dateRange"
          :unlink-panels="unlinkPanels"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="头像" align="center" prop="avator" >
        <template slot-scope="scope">
          <el-avatar shape="square" :size="60"  :src="ossPreview+ scope.row.avator"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="身份" align="center" prop="memPerm" :formatter="memPermFormat"/>
      <el-table-column label="状态" align="center" prop="isEnable" :formatter="enableFormat"/>
      <el-table-column label="标签" align="center" prop="label" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="注册时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listMember, getMember, delMember, addMember, updateMember, exportMember } from "@/api/member/member";
import defaultSettings from '@/settings'
import memberCoupon from './MemberCoupon'
import memberCar from './MemberCar'
import orderList from '../../order/order/index'
export default {
  name: "Member",
  props: {
    parentId: {
      type: Number,
      default: undefined
    }
  },
  components:{memberCoupon,memberCar,orderList},
  data() {
    return {
      memberId:null,
      ossPreview: defaultSettings.ossPreview,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      destroy: true,
      // 是否显示弹出层
      open: false,
      editopen: false,
      carOpen: false,
      orderOpen: false,
      tgOpen: false,
      // 查询参数
      dateRange: [],
      unlinkPanels:true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mobile: undefined,
        nickName: undefined,
        avator: undefined,
        openId: undefined,
        password: undefined,
        parentId: this.parentId,
        gmtTime: undefined,
        memPerm: undefined,
        isEnable: undefined,
        label: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      enableOptions: [],
      memPermOptions: [],
    };
  },
  created() {
    this.getList();

    this.getDicts("sys_is_enable").then(response => {
      this.enableOptions = response.data;
    });

    this.getDicts("mem_perm").then(response => {
      this.memPermOptions = response.data;
    });
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listMember(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 取消按钮
    editcancel() {
      this.editopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        mobile: undefined,
        nickName: undefined,
        avator: undefined,
        openId: undefined,
        password: undefined,
        parentId: undefined,
        gmtTime: undefined,
        unionid: undefined,
        memPerm: undefined,
        isEnable: undefined,
        label: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.editopen = true;
        this.title = "修改会员信息";
      });
    },
    /** 修改按钮操作 */
    handleCoupon(row) {
      this.reset();
      const id = row.id || this.ids
      this.memberId = row.id;
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "会员代金券";
      });
    },
    /** 修改按钮操作 */
    handleCar(row) {
      this.memberId = row.id;
      this.carOpen = true;
      this.title = "会员车辆";
    },
    /** 修改按钮操作 */
    handleOrder(row) {
      this.memberId = row.id;
      this.orderOpen = true;
      this.title = "会员订单";
    },

    /** 修改按钮操作 */
    handleTg(row) {
      this.memberId = row.id;
      this.tgOpen = true;
      this.title = "我的推广";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMember(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.editopen = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addMember(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.editopen = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMember(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMember(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    enableFormat(row, column) {
      return this.selectDictLabel(this.enableOptions, row.isEnable);
    },
    memPermFormat(row, column) {
      return this.selectDictLabel(this.memPermOptions, row.memPerm);
    }
  }
};
</script>
